package com.planet.api;


import com.planet.facade.SysUserBiz;
import com.planet.query.SysUserQuery;
import com.planet.utils.AssertUtils;
import com.planet.utils.UtilTools;
import com.planet.utils.global.ExceptionHandle;
import com.planet.utils.global.Result;
import com.planet.utils.global.ResultUtil;
import com.planet.vo.SysUserVO;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by jiayazi on 17/11/13.
 */
@RestController
@RequestMapping("/user")
public class UserAPI {
    private static final Logger LOGGER = LogManager.getLogger(UserAPI.class);

    @Autowired
    private ExceptionHandle exceptionHandle;
    @Autowired
    private SysUserBiz sysUserBiz;


    /**
     * 登陆
     * @param userName 用户Id
     * @return
     */
    @GetMapping("/login")
    public Result login(@ApiParam(name = "userName") @RequestParam String userName,@ApiParam(name = "passWord") @RequestParam String passWord){

        //默认结果是成功的
        Result result = ResultUtil.success();
        try {
            String userId = sysUserBiz.login(userName,passWord);
            if (StringUtils.isEmpty(userId)){
                result =  ResultUtil.error(1,"用户名或密码错误");
                return result;
            }

            SysUserVO sysUserVO = sysUserBiz.selectByPrimaryKey(userId);
            result =  ResultUtil.success(sysUserVO);

        }catch (Exception e){
            //调用异常工具类，统一处理异常
            result =  exceptionHandle.exceptionGet(e);
        }
        return result;
    }


    /**
     * 注册
     * @param sysUserQuery 用户Id
     * @return
     */
    @PostMapping("/register")
    public Result register(@ApiParam("注册信息") @RequestBody SysUserQuery sysUserQuery){

        AssertUtils.isNotNull(sysUserQuery.getLogin_name(),"登陆名不能为空");
        AssertUtils.isNotNull(sysUserQuery.getPassword(),"密码不能为空");
        //默认结果是成功的
        Result result = ResultUtil.success();
        try {

            sysUserQuery.setUserId(UtilTools.getUserId());
            sysUserQuery.setCompany_id("001");
            sysUserQuery.setOffice_id("001");
            sysUserQuery.setName(sysUserQuery.getLogin_name());
            sysUserQuery.setCreate_by("13757195640");
            sysUserBiz.insertSelective(sysUserQuery);
        }catch (Exception e){
            //调用异常工具类，统一处理异常
            result =  exceptionHandle.exceptionGet(e);
        }
        return result;
    }


}
